package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yu.crm.domain.pojo.CrmDepartmentPosition;
import com.yu.crm.mapper.CrmDepartmentPositionMapper;
import com.yu.crm.service.CrmDepartmentPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import com.yxzz.common.helper.ExceptionHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 部门职位表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmDepartmentPositionServiceImpl extends ServiceImpl<CrmDepartmentPositionMapper, CrmDepartmentPosition> implements CrmDepartmentPositionService {

    @Override
    public boolean saveOrUpdate(CrmDepartmentPosition entity) {
        if(DataHelpers.isEmpty(entity.getId())){
            QueryWrapper<CrmDepartmentPosition> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("department_id", entity.getDepartmentId()).eq("position_name", entity.getPositionName());
            int oldCount = super.count(queryWrapper);
            ValidatorHelpers.validateDataExist("职位名称不能重复", oldCount);
            entity.setGmtCreate(DateHelpers.now());
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.save(entity), "新增失败");
        }else{
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.updateById(entity), "修改失败");
        }
    }

    @Override
    public CrmDepartmentPosition getById(Serializable id) {
        return baseMapper.getById((Long) id);
    }

    @Override
    public IPage<CrmDepartmentPosition> list(IPage<CrmDepartmentPosition> page, CrmDepartmentPosition queryInfo) {
        /*QueryWrapper<CrmDepartmentPosition> queryWrapper = new QueryWrapper<>();
        if(DataHelpers.isNotEmpty(queryInfo.getDepartmentId())){
            queryWrapper.eq("department_id", queryInfo.getDepartmentId());
        }
        if(DataHelpers.isNotEmpty(queryInfo.getPositionName())){
            queryWrapper.like("position_name", queryInfo.getPositionName());
        }*/
        return baseMapper.list(page, queryInfo);
    }
}
